import {observable, action, runInAction} from 'mobx';

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class InputCarStore {
    @observable visible;
    @observable parkInfo;
    @observable value = ['粤', 'B', '', '', '', '', '', '']; //输入车牌值
    @observable index;
    @observable inputType;
    @observable disabled = true;
    vehicleNumber = '';

    @action
    closeSelect = () => {
        runInAction(() => {
            this.index = null;
            this.visible = false;
        });
    };

    @action
    fallback = () => {
        runInAction(() => {
            if (this.index > 0) {
                this.value[this.index] = '';
                this.index = --this.index;
            }
            this.vehicleNumber = this.getVehicleNumber();
        });
    };

    @action
    loadData = () => {
        let params = {};
        params.parkId = getUrlParam("parkId");
        postJson("/api/car/anon/getParkInfoByID", params, (data) => {
            runInAction(() => {
                this.parkInfo = data;
            });
        });
    };

    @action
    selectInput = (index) => {
        runInAction(() => {
            this.index = index;
            this.visible = true;
        });
    };

    @action
    carPay = (props) => {
        this.parkInfo.plateNumber = this.vehicleNumber;
        this.parkInfo.code = getUrlParam("code");
        props.carOrderStore.setOrderInfo(this.parkInfo, props);
        props.history.push('/payAuth/jhcloud/carOrder');
    };

    @action
    selectCallback = (ver) => {
        runInAction(() => {
            this.value[this.index] = ver;
            if (this.index < 7) {
                this.index = ++this.index;
            }
            this.vehicleNumber = this.getVehicleNumber();
        });
    };


    getVehicleNumber() {
        let vehicleNumber = '';
        let values = this.value.slice();
        for (let i in values) {
            vehicleNumber += values[i];
        }
        if (vehicleNumber.length === 8) {
            this.disabled = false;
        } else {
            this.disabled = !(vehicleNumber.length === 7 && !this.value[7]);
        }

        return vehicleNumber;
    }
}

export default new InputCarStore()
